<template>
	<view style="padding: 20rpx;">
		<u-form-item style="border-bottom: 1rpx solid #EEEEEE;" :label-width="150" label="手机号:"><u-input v-model="phone" :border='true' placeholder="请输入手机号"	/></u-form-item>
		<view>
			<view class="codeinput">
				<u-form-item style="border-bottom: 1rpx solid #EEEEEE;" :label-width="150" label="验证码:"><u-input v-model="captcha" :border='true' placeholder="请输入验证码"	/>
				<view class="wrap">
					<u-toast ref="uToast"></u-toast>
					<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
					<text @tap="getCode" style="color: #aaa4f6;font-size: 25rpx;">{{ tips }}</text>
				</view></u-form-item>
			</view>
		</view>
		<view class="btn_phone" @click="btnPhone">
			保存
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				seconds: 60,
				name:'',
				tips: '',
				phone:'',
				captcha:''
			};
		},
		methods:{
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				let data = {
					phone: this.phone
				};
				if(!this.phone){
					this.$refs.uToast.show({
						title: '请输入手机号',
						type: 'error'
					});
					return;
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					});
					this.$u.api.getcaptcha(data).then(res => {
						uni.hideLoading();
						this.$u.toast('验证码已发送');
						this.$refs.uCode.start();
					});
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				this.$u.toast('倒计时结束');
			},
			start() {
				this.$u.toast('倒计时开始');
			},
			btnPhone(){
				let data = {
					phone:this.phone,
					validation:this.captcha
				};
				if(!this.phone){
					this.$refs.uToast.show({
						title: '请输入手机号',
						type: 'error'
					});
					return;
				}
				if(!this.captcha){
					this.$refs.uToast.show({
						title: '请输入验证码',
						type: 'error'
					});
					return;
				}
				this.$u.api.getmodifyPhone(data).then(res => {
					this.$refs.uToast.show({
						title: '手机号修改成功',
						type: 'success'
					});
					uni.setStorage({
						key:'_PHONE_',
						data:this.phone
					})
				}).catch(e => {
					this.$refs.uToast.show({
						title: '手机号修改失败',
						type: 'error'
					});
				})
			}
		}
	}
</script>

<style lang="scss">
.wrap{
	padding: 0 20rpx;
}
.btn_phone{
	width: 80%;
		padding: 20rpx;
		background: #05B2B5;
		color: #fff;
		font-size: 30rpx;
		margin: 0 auto;
		text-align: center;
		border-radius: 50rpx;
		margin-top: 50rpx;
}
</style>
